<template>
  <div v-enter-helper="getList" class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handleAdd">充值</el-button>
          <!-- <el-button type="danger" @click="handleDeleteAll">批量删除</el-button> -->
          <el-button type="primary" @click="handleExport">导出</el-button>
          <el-form
            ref="dataList2"
            :inline="true"
            :model="listQuery"
            class="demo-form-inline"
          >
            <el-form-item label="">
              <el-input
                v-model="listQuery.user_code"
                clearable
                placeholder="请输入用户ID"
                type="text"
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                v-model="listQuery.nickname"
                clearable
                placeholder="请输入昵称"
                type="text"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="searchSubmit">查询</el-button>
              <el-button type="primary" @click="resetSubmit">重置</el-button>
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <div class="item">
        <el-table
          ref="dataList"
          v-loading="listLoading"
          :data="menuList"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          border
          row-key="id"
          style="width:100%;margin-bottom: 20px;"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection"></el-table-column>

          <el-table-column label="用户ID" prop="user_code"></el-table-column>
          <el-table-column label="昵称" prop="nickname"></el-table-column>
          <!-- <el-table-column label="用户头像" prop="nickname">
            <template slot-scope="{ row }">
              <img
                v-if="row.head_picture"
                :src="row.head_picture"
                alt=""
                style="width: 80px;height:80px;"
              />
            </template>
          </el-table-column> -->
          <el-table-column label="管理员名称" prop="admin_name">
          </el-table-column>
          <el-table-column label="充值金额" prop="gold"></el-table-column>
          <el-table-column label="充值类型" prop="type_name"></el-table-column>
          <el-table-column label="备注" prop="describe"></el-table-column>
          <el-table-column label="添加时间" prop="add_time"></el-table-column>
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button
                plain
                size="mini"
                type="primary"
                @click="handleDetail(scope.row)"
              >详情
              </el-button
              >
              <!-- <el-button
                icon="el-icon-delete"
                plain
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
              >删除
              </el-button> -->

            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :limit.sync="listQuery.perPage"
          :page.sync="listQuery.page"
          :total="total"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="确定删除吗?"
      width="435px"
      @close="handleDialogClose('editRulesForm')"
    >
      <span style=""
      ><em style="font-style:normal;color:red">删除后不可恢复</em
      >，你还要继续吗？</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 充值 -->
    <el-dialog :visible.sync="userFormDialogVisible" title="输入用户ID" width="30%">
      <el-form ref="userForm" :inline="false" :model="userForm" :rules="rules" label-width="120px" size="normal">
        <el-form-item label="用户ID：" prop="user_code">
          <el-input v-model="userForm.user_code" placeholder="请输入用户ID"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="userFormDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog :visible.sync="addDialogVisible" title="添加充值" width="30%">

      <section class=" flex flex-col pl-10">
        <section class="items flex items-center">
          <span>用户头像：</span>
          <el-avatar :src="userInfo.picture"></el-avatar>
        </section>

        <section class="flex items items-center">
          <section class="lf mr-10 flex">
            <span>用户名称：</span>
            <span>{{userInfo.nickname}}</span>
          </section>

          <section class="ri">
            <span>用户ID：</span>
            <span>{{ userInfo.user_code }}</span>
          </section>
        </section>

        <section class=" flex items items-center">
          <section class="lf mr-10 flex">
            <span>当前余额：</span>
            <span>{{ userInfo.user_coin }}</span>
          </section>

          <section class="ri">
            <span>当前收益：</span>
            <span>{{ userInfo.user_income }}</span>
          </section>
        </section>

      </section>

      <el-form ref="userForm" :inline="false" :model="rechargeForm" :rules="rechargeRules" label-width="120px" size="normal">

        <el-form-item label="充值金额：" prop="money">
          <el-input v-model="rechargeForm.money" placeholder="请输入充值金额">
            <el-button slot="append" style="background-color: #449ffb;color: #FFFFFF" type="primary"  @click="handleMax">一键最大值</el-button>
          </el-input>
        </el-form-item>

        <el-form-item label="充值类型：" prop="type">
          <el-radio-group v-model="rechargeForm.type">
            <el-radio :label="1">余额</el-radio>
            <el-radio :label="2">收益</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注：" prop="describe">
          <el-input
            v-model="rechargeForm.describe"
            :cols="3"
            type="textarea"
          />
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog :visible.sync="detailDialogVisible" title="详情" width="30%">

      <section class=" flex flex-col pl-10">
        <section class="items flex items-center">
          <span class="w-20 text-right">用户头像：</span>
          <el-avatar :src="userInfo.picture"></el-avatar>
        </section>

        <section class="flex items items-center">
          <section class="lf mr-10 flex w-1/3">
            <span class="w-20 text-right">用户ID：</span>
            <span>{{userInfo.user_code}}</span>
          </section>
          <section class="ri w-1/3 flex">
            <span class="w-20 text-right">用户名称：</span>
            <span>{{ userInfo.nickname }}</span>
          </section>
        </section>

        <section class=" flex items items-center">
          <section class="lf mr-10 flex w-1/3">
            <span class="w-20 text-right">当前余额：</span>
            <span>{{userInfo.user_coin}}</span>
          </section>

          <section class="ri w-1/3 flex">
            <span class="w-20 text-right">当前收益：</span>
            <span>{{ userInfo.user_income }}</span>
          </section>
        </section>

        <section class=" flex items items-center">
          <section class="lf mr-10 flex w-1/3">
            <span class="w-20 text-right">充值金额：</span>
            <span>{{userInfo.gold}}</span>
          </section>

          <section class="ri w-1/3 flex">
            <span class="w-20 text-right">充值类型：</span>
            <span>{{ userInfo.type_name }}</span>
          </section>
        </section>

        <section class=" flex items items-center">
          <section class="lf mr-10 flex">
            <span class="w-20 text-right">备注：</span>
            <span>{{userInfo.describe}}</span>
          </section>
        </section>
        <section class=" flex items items-center">
          <section class="lf mr-10 flex">
            <span class="w-20">充值时间：</span>
            <span>{{userInfo.add_time}}</span>
          </section>
        </section>

      </section>


      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="detailDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { deleteData } from '@/api/member/frozenIncomeList'
import { exportExcel, exportExcelAsync, adminRechargeList } from '@/api/member/adminRecharge'
import {
  listData,
  postRecharge
} from '@/api/member/listUsers'
export default {
  data() {
    return {
      detailDialogVisible: false,
      userInfo: {
        picture: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
        nickname: '',
        user_code: '',
        user_id: '',
        user_income: '',
        user_coin: '',
        money: ''
      },
      confirmBtn: false,
      userFormDialogVisible: false,
      row_state: undefined,
      row_id: undefined,
      cancelFreezeDialogVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      total: 10,
      listQuery: {
        page: 1,
        perPage: 10,
        nickname: undefined,
        user_id: undefined,
        admin_id: undefined
      },
      // 礼物列表
      typeList: [],
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: null,
      userForm: {
        user_code: '',
      },
      rules: {
        radio: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        user_code: [{ required: true, message: '请输入分类名', trigger: 'blur' }],
        remark: [{ required: false, message: '请输入备注', trigger: 'blur' }]
      },
      rechargeForm: {
        money: '',
        type: 1,
        describe: ''
      },
      rechargeRules: {
        type: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        money: [{ required: true, message: '请输入充值金额', trigger: 'blur' }],
        describe: [{ required: false, message: '请输入备注', trigger: 'blur' }],
      },
      addDialogVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleDetail(row) {
      listData({
        user_code: row.user_code,
        page: 1
      }).then(res => {
        if (res.code === 200) {
          this.detailDialogVisible = true
          this.userInfo = {
            picture: res.data.list[0].head_picture,
            nickname: res.data.list[0].nickname,
            user_code: res.data.list[0].user_code,
            user_id: res.data.list[0].user_id,
            user_income: res.data.list[0].earnings,
            user_coin: res.data.list[0].money,
            add_time: row.add_time,
            describe: row.describe,
            type_name: row.type_name,
            gold: row.gold
          }
        }
      })
    },
    getList() {
      this.listLoading = true

      adminRechargeList(this.listQuery).then(response => {
        this.menuList = response.data.list
        this.total = response.data.meta.total
        this.listLoading = false
        this.confirmBtn = false
      })
    },
    //充值按钮 一键最大值 10000000
    handleMax() {
      this.rechargeForm.money = 10000000
    },
    handleAdd() {
      this.userForm.user_code = ''
      this.userFormDialogVisible = true
    },
    handleConfirm() {
      this.$refs['userForm'].validate((valid) => {
        if (valid) {
          postRecharge(this.user_id,this.rechargeForm).then(res => {
            if (res.code === 200) {
              this.userFormDialogVisible = false
              this.addDialogVisible = false
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              this.getList()
            }
          })

        } else {
          return false
        }
      })
    },
    confirmAdd() {
      this.$refs['userForm'].validate((valid) => {
        if (valid) {
          listData(this.userForm).then(res => {
            if (res.code === 200) {
              this.addDialogVisible = true
              this.userInfo.picture = res.data.list[0].head_picture
              this.userInfo.nickname = res.data.list[0].nickname
              this.userInfo.user_code = res.data.list[0].user_code
              this.userInfo.user_id = res.data.list[0].user_id
              this.userInfo.user_income = res.data.list[0].earnings
              this.userInfo.user_coin = res.data.list[0].money
              this.user_id = res.data.list[0].user_id
            }
          })

        } else {
          // 'error submit!!');
          return false
        }
      })
    },
    // 查询
    searchSubmit() {
      this.listQuery.page = 1
      this.getList()
    },
    // 重置
    resetSubmit() {
      this.listQuery.page = 1
      this.listQuery.perPage = 10
      this.listQuery.nickname = undefined
      this.listQuery.user_id = undefined
      this.getList()
    },

    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = { ids: row.id.toString() }
      // this.deleteDialogVisible = true
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请先勾选要删除的内容',
          type: 'warning'
        })
        return
      } else if (this.multipleSelection.length !== 0) {
        let ids = ''
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`
        }
        ids = ids.substring(0, ids.length - 1)
        this.dataIds = { ids }
      }
      // this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      if (this.confirmBtn) return
      this.confirmBtn = true
      deleteData(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.confirmBtn = false
        }
      })
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    },
    /**导出 */
    handleExport() {

      this.$confirm('确认导出当前列表数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let obj = {
          sociaty_no: undefined,
          user_id: undefined,
          nickname: undefined
        }
        for (let key in obj) {
          obj[key] = this.listQuery[key]
        }
        exportExcel(obj).then(res => {
          if (res.code === 200) {
            window.location.href = res.data.download_url
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    }
  }
}
</script>

<style scoped>
[v-cloak] {
  display: none;
}

.items {
  margin-bottom: 20px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

/deep/ .el-card__header {
  padding-bottom: 0;
}
</style>
